using System;
using System.Data;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A server-side or client side script stored in the database.
	/// </summary>
	public class Script : DatabaseAware, IRefreshable
	{
    private AttachmentType type;
    private string owner;
		internal Script(Server server, IDataReader reader) : base(server)
		{
      string key = reader["eKey"].ToString();
      Attachment.ParseKey(key, out type, out owner, out name);
    }

    private string name;
    /// <summary>
    /// Gets the name of the script.
    /// </summary>
    public string Name
    {
      get
      {
        return name;
      }
    }

    private string text;
    /// <summary>
    /// Gets the text of the script.
    /// </summary>
    public string Text
    {
      get
      {
        if (text == null)
        {
          byte[] scriptBytes = Server.GetAttachment(type, name, owner);
          text = Attachment.ByteArrayToString(scriptBytes);
        }
        return text;
      }
    }

    #region IRefreshable Members

    /// <summary>
    /// Refreshes the script contents.
    /// </summary>
    public void Refresh()
    {
      text = null;
    }

    #endregion
  }
}
